package py.edu.ucsa.sisale.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import py.edu.ucsa.sisale.dto.ModuloDTO;
import py.edu.ucsa.sisale.util.JaxwsJSONTranslator;
import py.edu.ucsa.sisale.util.ServiceHelper;

@Component
public class ModulosServiceClient extends ServiceHelper{
	
	private static final long serialVersionUID = 2894283845973378424L;
	
    private static Logger log = LoggerFactory.getLogger(ModulosServiceClient.class);
    private JaxwsJSONTranslator<ModuloDTO> translator = new JaxwsJSONTranslator<ModuloDTO>();
    
  //URL RAIZ DEL DOMINIO EN EL LADO REST, ej. /modulo, /proyecto, /usuario, SE A�ADIR� A: http://localhost:8080/sisalews
    private final static String moduleUri = "/modulo"; 
	
	public ModuloDTO getById(int id) {
        try{
            String jsondata = super.find(projectURI + moduleUri + "/id", id); //http://localhost:8080/sisalews/modulo/id
            return translator.fromJSON(jsondata, ModuloDTO.class);
        }
        catch (Exception e){
        	log.error(e.getMessage());
        }
        return null;
    }
	
	public List<ModuloDTO> listar() { //http://localhost:8080/sisalews/modulo
        try{
            String jsondata = super.list(projectURI + moduleUri);
            return translator.toList(jsondata, ModuloDTO.class);
        	
        }
        catch (Exception e){
        	e.printStackTrace();
        	log.error(e.getMessage());
        }
        return null;
    }

	public ModuloDTO guardar(ModuloDTO formData) throws Exception {
		String jsondata = null;
		try{
			jsondata = super.createOrUpdate(projectURI + moduleUri, translator.toJSON(formData));
			formData = translator.fromJSON(jsondata, ModuloDTO.class);
			String msg = "INSERCION EXITOSA: " + ": " + formData.getClass().getName() + "::id->" + formData.getId();
            log.info(msg);
            return formData;
        }
        catch (Exception e){
        	log.error(e.getMessage());
        	throw e;
        }
		
	}
	
	public ModuloDTO actualizar(ModuloDTO formData) throws Exception {
		String jsondata = null;
		try{
			jsondata = super.update(projectURI + moduleUri, translator.toJSON(formData));
			formData = translator.fromJSON(jsondata, ModuloDTO.class);
			String msg = "ACTUALIZACION EXITOSA: " + ": " + formData.getClass().getName() + "::id->" + formData.getId();
            log.info(msg);
            return formData;
        }
        catch (Exception e){
        	log.error(e.getMessage());
        	throw e;
        }
		
	}
	
	public void eliminar(Long id) throws Exception {
		try{
			super.delete(projectURI + moduleUri, id);
			String msg = "ELIMINACION EXITOSA: " + "::id->" + id;
            log.info(msg);
        }
        catch (Exception e){
        	log.error(e.getMessage());
        	throw e;
        }
	}

}